package com.ruoyi.project.system.submit.service.impl;

import com.ruoyi.project.system.dict.utils.DictUtils;

import com.ruoyi.project.system.submit.bean.MiniBean;
import com.ruoyi.project.system.submit.domain.MiniDetailDB;
import com.ruoyi.project.system.submit.domain.MiniDetailData;
import com.ruoyi.project.system.submit.domain.MiniIncomeData;
import com.ruoyi.project.system.submit.mapper.MiniSubmitMapper;
import com.ruoyi.project.system.submit.service.IMiniSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class MiniSubmitServiceImpl implements IMiniSubmitService {

    @Autowired
    private MiniSubmitMapper miniSubmitMapper;

    @Override
    public int getIncomeDataId(String fid,int reportYear) {
        return miniSubmitMapper.MiniSelectDataIdByFid(fid,reportYear);
    }

    @Override
    public int backShowId(String fid, int reportYear) {
        return miniSubmitMapper.backShowId(fid,reportYear);
    }

    @Override
    public Double backShowExpense(String fid, int reportYear) {
        return miniSubmitMapper.backShowExpense(fid,reportYear);
    }

    @Override
    public Integer getNumPerson(String fid) {
        return miniSubmitMapper.getNumPerson(fid);
    }

    @Override
    public List<MiniDetailData> backShowDetail(int incomeDataId,String fid, int reportYear) {
        List<MiniDetailData> miniDetailData = miniSubmitMapper.backShowDetail(incomeDataId);
        for (MiniDetailData miniDetailDatum : miniDetailData) {
            if(miniDetailDatum.getItemProduction()==null){
                miniDetailDatum.setItemProduction(String.valueOf(0));
            }
            BigDecimal bigDecimalV = new BigDecimal(miniDetailDatum.getItemValue());
            BigDecimal bigDecimalP = new BigDecimal(miniDetailDatum.getItemProduction());
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            miniDetailDatum.setItemProduction(decimalFormat.format(bigDecimalP));
            miniDetailDatum.setItemValue(decimalFormat.format(bigDecimalV));
            if (bigDecimalP.compareTo(BigDecimal.valueOf(0.00))==0){
                miniDetailDatum.setItemProduction("");
            }
            if (bigDecimalV.compareTo(BigDecimal.valueOf(0.00))==0){
                miniDetailDatum.setItemValue("");
            }
            switch (miniDetailDatum.getPrimaryClassification()) {
                case "4":
                    miniDetailDatum.setPrimaryClassification("转移性收入");
                    miniDetailDatum.setSecondClassification(DictUtils.getDictLabel("transfer_income_second_type", miniDetailDatum.getSecondClassification()));
                    miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("other_transfer_income_type", miniDetailDatum.getThirdClassification()));
                    if (miniDetailDatum.getThirdClassification().equals("---请选择---"))
                        miniDetailDatum.setThirdClassification("");
                    break;
                case "1":
                    miniDetailDatum.setPrimaryClassification("生产经营性收入");
                    switch (miniDetailDatum.getSecondClassification()) {
                        case "1":
                            miniDetailDatum.setSecondClassification("种植业收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("planting_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                        case "2":
                            miniDetailDatum.setSecondClassification("养殖业收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("aquaculture_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                        case "3":
                            miniDetailDatum.setSecondClassification("第三产业收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("tertiary_industry_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                    }
                    break;
                case "2":
                    miniDetailDatum.setPrimaryClassification("财产性收入");
                    switch (miniDetailDatum.getSecondClassification()) {
                        case "1":
                            miniDetailDatum.setSecondClassification("其他财产性收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("other_property_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                        case "2":
                            miniDetailDatum.setSecondClassification("直接分红收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("direct_dividend_income", miniDetailDatum.getThirdClassification()));
                            break;
                    }
                    break;
                case "3":
                    miniDetailDatum.setPrimaryClassification("工资性收入");
                    switch (miniDetailDatum.getSecondClassification()) {
                        case "1":
                            miniDetailDatum.setSecondClassification("外出务工收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("migrant_workers_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                        case "2":
                            miniDetailDatum.setSecondClassification("村级公益岗收入");
                            miniDetailDatum.setThirdClassification(DictUtils.getDictLabel("village_welfare_income_type", miniDetailDatum.getThirdClassification()));
                            break;
                    }
                    break;
            }
        }
        Double expense = miniSubmitMapper.backShowExpense(fid, reportYear);

        System.out.println("支出为"+expense);
//        System.out.println(miniDetailData);
        return miniDetailData;
    }




    @Override
    public void enterDetail(List<MiniDetailData> realArray, int dataId) {
        List<MiniDetailDB> miniList = new ArrayList<>();

        String dictTypeTwo = null;
        String dictTypeThree = null;

        System.out.println(realArray);
        for (MiniDetailData item : realArray) {
            String primaryKind = item.getPrimaryClassification();
            String kind = item.getSecondClassification();
            String name = item.getThirdClassification();
            String amountData = item.getItemValue();
            String quantityData = item.getItemProduction();
            Double quantity;
            if(quantityData.length()==0){
                quantity = null;
            }else {
                quantity = Double.parseDouble(quantityData);
            }
            double amount;
            if(amountData.length()==0){
                amount = 0;
            }else {
                amount = Double.parseDouble(amountData);
            }


            int first = Integer.parseInt(DictUtils.getDictValue("first_category_type", MiniBean.map.get(primaryKind)));
            if (first == 4) {
                dictTypeTwo = "transfer_income_second_type";
            } else if (first == 1) {
                dictTypeTwo = "production_operation_income_second_type";
            } else if (first == 3) {
                dictTypeTwo = "salary_income_second_type";
            } else if (first == 2) {
                dictTypeTwo = "property_income_second_type";
            }
            //二级键值
            int second = Integer.parseInt(DictUtils.getDictValue(dictTypeTwo, MiniBean.map.get(kind)));
            //三级键值
            int third = 0;
            if (first == 4 && second == 1) {
                dictTypeThree = "other_transfer_income_type";
                third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
            } else if (first == 1) {
                if(second == 1){
                    dictTypeThree = "planting_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                } else if (second == 2) {
                    dictTypeThree = "aquaculture_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                } else if (second == 3) {
                    dictTypeThree = "tertiary_industry_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                }
            } else if (first ==2) {
                if(second ==1){
                    dictTypeThree = "other_property_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                } else if (second == 2) {
                    dictTypeThree = "direct_dividend_income";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                }
            } else if (first == 3) {
                if(second == 1){
                    dictTypeThree = "migrant_workers_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                } else if (second == 2) {
                    dictTypeThree = "village_welfare_income_type";
                    third = Integer.parseInt(DictUtils.getDictValue(dictTypeThree, name));
                }
            }
            System.out.println(first);
            System.out.println(second);
            System.out.println(third);
            System.out.println(quantity);
            System.out.println(amount);



            Integer id = miniSubmitMapper.MiniSelectDetailDBID(dataId, first, second, third);
//            System.out.println(id);

            MiniDetailDB db=new MiniDetailDB(id,dataId,first,second,third,quantity,amount);
            miniList.add(db);

        }
        System.out.println(miniList);
        miniSubmitMapper.entering(miniList);
    }

    /**
     * 在数据库表中更新数据
     * @param fid 户编号
     * @param yearBg 操作年份
     * @param incomeDataId 用于计算金额
     */
    @Override
    public void enterData(String fid, int yearBg, int incomeDataId, String operateTime,int numPerson,Double expenditureAmount,int userId) {
        double opSum = 0;
        double propertySum = 0;
        double salarySum = 0;
        double transferSum = 0;
        double sumIncome = 0;
        List<Double> opIncome = miniSubmitMapper.getOpIncome(incomeDataId, 1);
        for (double item : opIncome) {
            opSum += item;
        }
        List<Double> propertyIncome = miniSubmitMapper.getPropertyIncome(incomeDataId, 2);
        for (double item : propertyIncome) {
            propertySum += item;
        }
        List<Double> salaryIncome = miniSubmitMapper.getSalaryIncome(incomeDataId, 3);
        for (double item : salaryIncome) {
            salarySum += item;
        }
        List<Double> transferIncome = miniSubmitMapper.getTransferIncome(incomeDataId, 4);
        for (double item : transferIncome) {
            transferSum += item;
        }
        sumIncome = opSum + propertySum + salarySum + transferSum;
        MiniIncomeData miniIncomeData1 = new MiniIncomeData(fid, sumIncome, yearBg);
        miniSubmitMapper.enterData(miniIncomeData1);
        Double sumIncome1 = miniSubmitMapper.getSumIncome(fid, yearBg);
        System.out.println(sumIncome);
        if(sumIncome1==null){
            sumIncome1 = opSum + propertySum + salarySum + transferSum;
        }
        System.out.println("总收入"+sumIncome);
        System.out.println("生产性"+opSum);
        System.out.println("财产性"+propertySum);
        System.out.println("工资性"+salarySum);
        System.out.println("转移性"+transferSum);
        System.out.println("人口数"+numPerson);
        System.out.println("支出"+expenditureAmount);
        double avgIncome = 0;
        avgIncome = (sumIncome1 - expenditureAmount) / numPerson;
        System.out.println("平均收入"+avgIncome);
        MiniIncomeData miniIncomeData = new MiniIncomeData(fid,opSum,propertySum,salarySum,transferSum,sumIncome1,userId,yearBg,operateTime,expenditureAmount,avgIncome);
        miniSubmitMapper.enterData(miniIncomeData);
    }


}
